import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import dts from 'vite-plugin-dts';
import { resolve } from 'node:path';

console.log('resolve', resolve(__dirname, './src/components'));
// https://vitejs.dev/config/
export default defineConfig({
	plugins: [
		vue(),
		dts({
			root: resolve(process.cwd()), // 显式设置项目根目录
			include: ['packages/**'],
			exclude: [],
			outDir: resolve(process.cwd(), './dist/packages'),
			cleanVueFileName: true,
			compilerOptions: {
				skipLibCheck: true, // 跳过第三方库类型检查
				preserveSymlinks: true, // 保持符号链接结构
			},
			pathsToAliases: true,
			copyDtsFiles: true,
			staticImport: true,
			insertTypesEntry: true, // 生成类型入口
			//指定使用的tsconfig.json为我们整个项目根目录下掉,如果不配置,你也可以在components下新建tsconfig.json
			tsconfigPath: './tsconfig.json',
		}),
	],
	build: {
		//打包文件目录
		//压缩
		minify: true,
		outDir: './dist', //输出文件名称
		//css分离
		//cssCodeSplit: true,
		rollupOptions: {
			// 确保外部化处理那些你不想打包进库的依赖
			external: [
				'vue',
				'vue-router',
				'comment-parser',
				'markdown-it',
				'codemirror',
				/@codemirror/,
				/^monaco-editor/,
				/^prettier/,
				/^@fangzhongya/,
				// /^@highlightjs/,
				// /^highlight.js/,
			],
			//忽略打包vue文件
			// input: [],
			output: [
				{
					format: 'es',
					//不用打包成.es.js,这里我们想把它打包成.js
					entryFileNames: '[name].js',
					//让打包目录和我们目录对应
					preserveModules: true,
					assetFileNames: 'index.css',
					//配置打包根目录
					dir: resolve(process.cwd(), './dist'),
					preserveModulesRoot: 'dist',
				},
				{
					format: 'cjs',
					entryFileNames: '[name].cjs',
					//让打包目录和我们目录对应
					preserveModules: true,
					assetFileNames: 'index.css',
					//配置打包根目录
					dir: resolve(process.cwd(), './dist'),
					preserveModulesRoot: 'dist',
				},
			],
		},
		lib: {
			entry: './packages/index.ts',
		},
	},
	resolve: {
		alias: {
			vue: 'vue/dist/vue.esm-bundler.js',
			'@': resolve(process.cwd(), './src/components'),
		},
	},
	server: {
		host: '0.0.0.0', // 可以外部访问
		hmr: true, // 开启热更新
	},
});
